/*
*
*  广告数据模型
*  负责广告部分数据
*
*
*
*/
const db = require('../../config/db')
const fs = require('fs')
module.exports = {
    // 编写添加广告的方法
    // 参数：advert_id, advert_title, advert_href, advert_position, advert_src, advert_create_time, user_id,advert_text
    publish(advert_id, advert_title, advert_href, advert_position, advert_src, advert_create_time, user_id, advert_text) {
        return new Promise((resolve, reject) => {
            let advert_insert_sql = "insert into db_advert(advert_id, advert_title, advert_href, advert_position, advert_src, advert_create_time,user_id,advert_text) values('" + advert_id + "','" + advert_title + "','" + advert_href + "','" + advert_position + "','" + advert_src + "','" + advert_create_time + "','" + user_id + "','" + advert_text + "')"
            db.query(advert_insert_sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '添加广告失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '添加广告成功。',
                        advert_src,
                        advert_href
                    })
                }
            })
        })
    },

    // 编写修改广告的方法
    // 参数：advert_id, advert_title, advert_href, advert_position, advert_src, advert_create_time
    modify(advert_id, advert_title, advert_href, advert_position, advert_src, advert_create_time) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_advert where advert_id ='" + advert_id + "'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询广告失败。'
                    })
                } else {
                    if (rows.length != 0) {
                        var advert_update_sql = "update db_advert set advert_title = '" + advert_title + "',advert_position= '" + advert_position + "',advert_src='" + advert_src + "',advert_href='" + advert_href + "',advert_create_time = '" + advert_create_time + "' where  advert_id = '" + advert_id + "'";
                        db.query(advert_update_sql, function (err) {
                            if (err) {
                                resolve({
                                    code: -1,
                                    msg: "修改广告失败。",
                                    err
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "修改广告成功。"
                                });
                            }
                        });
                    }
                }
            })
        })
    },

    // 编写删除广告的方法
    // 参数：advert_id
    remove(advert_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_advert where advert_id='" + advert_id + "'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询失败。'
                    })
                } else {
                    if (rows.length != 0) {
                        let advert_src = rows[0].advert_src
                        let index = advert_src.indexOf('upload')
                        let filePath = `./public/${advert_src.slice(index)}`
                        console.log("filePath:", filePath)
                        let stats = fs.statSync(filePath)
                        // 判断是否是文件
                        if (stats.isFile()) {
                            // 删除文件
                            fs.unlinkSync(filePath)
                            db.query("delete from  db_advert where advert_id = '" + advert_id + "'", function (err) {
                                if (err) {
                                    resolve({
                                        code: -1,
                                        msg: '删除广告失败1。'
                                    })
                                } else {
                                    resolve({
                                        code: 200,
                                        msg: '删除广告成功。'
                                    })
                                }
                            });
                        } else {
                            resolve({
                                code: -1,
                                msg: '删除广告失败2。'
                            })
                        }
                    }
                }
            })
        })
    },

    // 编写获取广告列表的方法
    // 参数：
    list() {
        return new Promise((resolve, reject) => {
            db.query('select * from db_advert', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取广告列表失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '获取广告列表成功。',
                        result: rows
                    })
                }
            })
        })
    },

    // 编写获取广告详情的方法
    // 参数：advert_id
    detail(advert_id) {
        return new Promise((resolve, reject) => {
            // 查询语句
            let sql = 'select * from db_advert where advert_id = "' + advert_id + '"'
            // 调用查询方法
            db.query(sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询广告失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows,
                        msg: '查询广告成功。'
                    });
                }
            });
        })
    },

    // 编写示例xx方法
    // 参数：keyword
    search(keyword) {
        return new Promise((resolve, reject) => {
            let sql = "select * from db_advert where advert_title LIKE CONCAT('%" + keyword + "%') or advert_href LIKE CONCAT('%" + keyword + "%') or advert_position LIKE CONCAT('%" + advert_position + "%')";
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: 1,
                            msg: '没有该关键字匹配的内容。'
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 编写删除图片的方法
    delete(advert_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_advert where advert_id='" + advert_id + "'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询失败。'
                    })
                } else {
                    // console.log("rows:", rows)
                    if (rows.length != 0) {
                        let advert_src = rows[0].advert_src
                        let index = advert_src.indexOf('upload')
                        let filePath = `./public/${advert_src.slice(index)}`
                        let stats = fs.statSync(filePath)
                        console.log("delete-->filePath:", filePath)
                        // 判断是否是文件
                        if (stats.isFile()) {
                            // 删除文件
                            fs.unlinkSync(filePath)
                            resolve({
                                code: 200,
                                msg: '修改时,删除广告图片成功。'
                            })
                        } else {
                            resolve({
                                code: -1,
                                msg: '删除广告图片失败2。'
                            })
                        }
                    }else {
                        resolve({
                            code: 404,
                            msg: '找不到图片。',
                            result:rows
                        })
                    }
                }
            })
        })
    },
}